import codecs

from cfg import Const
from core.util import TimeUtil

#日志目录
LOG_BASE = './log/'


def Write(s, app_name, nowTime=None, file_name=None):
    #获取日志记录时间
    if nowTime is None:
        nowTime = TimeUtil.GetNowTime()
    #获取文件路径
    if file_name is None:
        preName = nowTime
        if len(nowTime)>10:
            preName = nowTime[0:7]
        file_name = LOG_BASE + app_name + '-' + preName + '.log'

    if Const.DEBUG == 1:
        #测试环境
        #拼接日志数据
        logstr = str('\n\n' + nowTime + '\n' + s)
        f = None
        try:
            f = codecs.open(file_name, 'a+', 'utf-8')
            f.write(logstr)
            f.close()
        except IOError as e:
            pass
    elif Const.DEBUG == 2:
        #开发环境
        logstr = str(nowTime + '\n' + s)
        print(logstr)
    else:
        #正式环境
        pass


#日志读取，待开发
def Readone(n):
    pass

# 日志归档
def ArchiveLogs(app_name, period=90):
    pass